package com.tally.server.service;

import com.tally.server.domain.Likes;
import com.tally.server.domain.LikesExample;
import com.tally.server.dto.LikesDTO;
import com.tally.server.dto.PageDTO;
import com.tally.server.dto.UserDTO;
import com.tally.server.enums.LikeStatusEnum;
import com.tally.server.mapper.LikesMapper;
import com.tally.server.utils.CopyUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Date;

@Service
public class LikesService {
    private static final Logger logger = LoggerFactory.getLogger(LikesService.class);

    @Autowired
    private LikesMapper likesMapper;

    @Autowired
    private UserService userService;

    /**
     * 列表查询
     */
    public void list(PageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPage(), pageDTO.getSize());
        LikesExample likesExample = new LikesExample();
        List<Likes> likesList = likesMapper.selectByExample(likesExample);
        PageInfo<Likes> pageInfo = new PageInfo<>(likesList);
        pageDTO.setTotal(pageInfo.getTotal());
        List<LikesDTO> likesDTOList = CopyUtil.copyList(likesList, LikesDTO.class);
        pageDTO.setList(likesDTOList);
    }

    /**
     * 保存，id有值时更新，无值时新增
     */
    public void save(LikesDTO likesDTO) {
        Likes likes = CopyUtil.copy(likesDTO, Likes.class);
        Likes likesDb = likesMapper.selectByPrimaryKey(likesDTO.getUserId(), likesDTO.getPostId());
        if (likesDb == null) {
            this.insert(likes);
        } else {
            this.update(likes);
        }
    }

    /**
     * 新增
     */
    private void insert(Likes likes) {
        Date now = new Date();
        likes.setCreateTime(now);
        likes.setUpdateTime(now);
        likesMapper.insert(likes);
    }

    /**
     * 更新
     */
    private void update(Likes likes) {
        likes.setUpdateTime(new Date());
        likesMapper.updateByPrimaryKeySelective(likes);
    }

    public List<LikesDTO> getByPostId(String postId) {
        LikesExample likesExample = new LikesExample();
        likesExample.createCriteria()
                .andPostIdEqualTo(postId)
                .andLikeStatusEqualTo(LikeStatusEnum.LIKE.getCode());
        List<Likes> likesList = likesMapper.selectByExample(likesExample);
        List<LikesDTO> likesDTOList = CopyUtil.copyList(likesList, LikesDTO.class);
        likesDTOList.forEach(likesDTO -> {
            UserDTO userDTO = userService.getById(likesDTO.getUserId());
            likesDTO.setUser(userDTO);
        });
        return likesDTOList;
    }

    public LikesDTO getByUserIdAndPostId(Integer userId, String postId) {
        Likes likes = likesMapper.selectByPrimaryKey(userId, postId);
        return CopyUtil.copy(likes, LikesDTO.class);
    }

    public void deleteByPostId(String postId) {
        LikesExample likesExample = new LikesExample();
        likesExample.createCriteria().andPostIdEqualTo(postId);
        int i = likesMapper.deleteByExample(likesExample);
        logger.info("已删除{}条点赞信息", i);
    }
}
